package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.DeviceType;
import diploma.kisel.modelas.repository.api.DeviceTypeRepository;

public class DeviceTypeRepositoryImpl implements DeviceTypeRepository {

	private EntityManager em = HibernateUtil.getEm();

	@Override
	public List<DeviceType> findAll() {
		List<DeviceType> types = em.createNamedQuery("DeviceType.findAll",
				DeviceType.class).getResultList();
		return types;
	}

	@Override
	public DeviceType save(DeviceType type) {
		em.getTransaction().begin();
		em.persist(type);
		em.getTransaction().commit();
		return type;
	}

	@Override
	public DeviceType update(DeviceType type) {
		em.getTransaction().begin();
		em.merge(type);
		em.getTransaction().commit();
		return type;
	}

	@Override
	public void remove(DeviceType type) {
		em.getTransaction().begin();
		em.remove(type);
		em.getTransaction().commit();
	}

	@Override
	public DeviceType findById(String id) {
		TypedQuery<DeviceType> query = em.createNamedQuery("DeviceType.findById", DeviceType.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

}
